package com.boardour.ffmpegdemo;

import android.Manifest;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.boardour.ffmpegdemo.permission.XPermission;
import com.boardour.ffmpegdemo.permission.XPermissionListener;
import com.boardour.ffmpegdemo.player.FFPlayer;
import com.boardour.ffmpegdemo.player.OnErrorListener;
import com.boardour.ffmpegdemo.player.OnPrepareListener;

import java.io.File;

/**
 * 基于FFMPEG，视频解码显示
 */
public class VideoActivity extends AppCompatActivity implements OnPrepareListener, XPermissionListener, OnErrorListener {
    private int requestCode = 0x100;
    private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
    // 播放器 java层
    private FFPlayer player;
    private TextView tv;
    private SurfaceView surfaceView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        initData();
        initViews();
    }

    private void initData() {
        player = new FFPlayer();
        // 解析视频，准备完成回调
        player.setOnPrepareListener(this);
        // 错误回调
        player.setOnErrorListener(this);
        // 绑定Activity生命周期，释放资源
        getLifecycle().removeObserver(player);
    }

    private void initViews() {
        tv = findViewById(R.id.tv);
        surfaceView = findViewById(R.id.surface_view);
        player.setSurfaceView(surfaceView);
    }

    public void start(View view) {
        if (XPermission.hasPermissions(this, permissions)) {
            start();
        } else {
            XPermission.requestPermissions(this, requestCode, permissions);
        }
    }

    private void start() {
        String path = new File(Environment.getExternalStorageDirectory() + File.separator + "Movies/3d.mp4")
                .getAbsolutePath();
        Log.e("TAG", path);
        player.setPath(path);
        player.prepare();
    }

    /**
     * 准备完成，可以开始播放
     */
    @Override
    public void onPrepared() {
        runOnUiThread(() -> {
            tv.setTextColor(Color.GREEN);
            tv.setText("初始化视频成功...");
        });
        player.start();
    }

    @Override
    public void onError(int code, String msg) {
        runOnUiThread(() -> {
            tv.setTextColor(Color.RED);
            tv.setText(msg);
        });
    }

    @Override
    public void onSucceed() {
        start();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (this.requestCode == requestCode) {
            XPermission.onRequestPermissionsResult(grantResults, this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        player.release();
    }
}
